//
//  PickView.m
//  QinjinProject
//
//  Created by Michael on 14-12-26.
//  Copyright (c) 2014年 ___Michael___. All rights reserved.
//

#import "PickView.h"

@implementation PickView

-(id)init
{
    self = [super init];
    if (self)
    {
        [self loadView];
    }
    return self;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
      [self loadView];
    }
    return self;
}

-(void)awakeFromNib
{
    [super awakeFromNib];
    
    [self loadView];
}

-(void)loadView
{
    UIView * viewTitle = [[UIView alloc]initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 44)];
    viewTitle.backgroundColor = UIColorFromRGB(0x1F,0xab,0xa0);
    [self addSubview:viewTitle];
    
    self.pickTitle = [[UILabel alloc]initWithFrame:CGRectMake(70, 0,viewTitle.width-(70*2), 44)];
    self.pickTitle.text = @"选择时间";
    self.pickTitle.textColor = [UIColor whiteColor];
    self.pickTitle.font = [UIFont systemFontOfSize:15];
    [self.pickTitle setTextAlignment:NSTextAlignmentCenter];
    [viewTitle addSubview:self.pickTitle];
    
    UIButton * btnOK = [[UIButton alloc]initWithFrame:CGRectMake(viewTitle.width-60, 0, 60, 44)];
    [btnOK addTarget:self action:@selector(confirmChangeData:) forControlEvents:UIControlEventTouchUpInside];
    [btnOK setTitle:@"确定" forState:UIControlStateNormal];
    [viewTitle addSubview:btnOK];
    
    UIButton * btnCencel = [[UIButton alloc]initWithFrame:CGRectMake(0, 0,60, 44)];
    [btnCencel addTarget:self action:@selector(canelChangeData:) forControlEvents:UIControlEventTouchUpInside];
    [btnCencel setTitle:@"取消" forState:UIControlStateNormal];
    
    [viewTitle addSubview:btnCencel];
}


-(void)setDataPick
{
 if (!datePickerBirthday)
 {
    datePickerBirthday = [[UIDatePicker alloc] init];
    if (self.pickMaxData)
    {
      [datePickerBirthday setMaximumDate:self.pickMaxData];
    }
    if (self.pickMinData)
    {
      [datePickerBirthday setMinimumDate:self.pickMinData];
    }
    [datePickerBirthday setBackgroundColor:[UIColor whiteColor]];
//    [datePickerBirthday setDate:[NSDate date] animated:NO];
    [datePickerBirthday setDatePickerMode:UIDatePickerModeDate];
    [datePickerBirthday addTarget:self action:@selector(setBirthdayDateWithPickerDate) forControlEvents:UIControlEventValueChanged];
    [datePickerBirthday setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self addSubview:datePickerBirthday];
     
    NSDictionary * views = NSDictionaryOfVariableBindings(datePickerBirthday);
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0@1000-[datePickerBirthday]-0@1000-|"
                                                                           options:0
                                                                           metrics:nil
                                                                             views:views]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-44@1000-[datePickerBirthday]-0@1000-|"
                                                                           options:0
                                                                           metrics:nil
                                                                             views:views]];
  }
}

-(void)setPickViewMaxData:(NSDate *)pickMaxData
{
    [datePickerBirthday setMaximumDate:pickMaxData];
}

-(void)setPickViewMinData:(NSDate *)pickMinData
{
    [datePickerBirthday setMinimumDate:pickMinData];
}

-(void)setDefoultData:(NSDate *)pickMinData
{
   [datePickerBirthday setDate:pickMinData animated:YES];
}

-(void)canelChangeData:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(toolBarCanelClick:)])
    {
        [self.delegate toolBarCanelClick:sender];
    }
    
    if ([self.delegate respondsToSelector:@selector(showPickerData:)])
    {
        [self.delegate showPickerData:NO];
    }
}

-(void)confirmChangeData:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(toolBarDoneClick:)])
    {
        [self.delegate toolBarDoneClick:sender];
    }
    
    if ([self.delegate respondsToSelector:@selector(showPickerData:)])
    {
        [self.delegate showPickerData:NO];
    }
}

-(void)setBirthdayDateWithPickerDate
{
    self.pickTitle.text =  [CommonFunctions convertDate:datePickerBirthday.date withFormatter:@"yyyy-MM-dd" subFromIndex:0];
}

//-(void)showPickerData:(BOOL)show
//{
//
//}


@end
